package club.gskj.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import club.gskj.gulimall.product.entity.ProductAttrValueEntity;
import club.gskj.gulimall.product.service.ProductAttrValueService;
import club.gskj.gulimall.product.vo.AttrRespVo;
import club.gskj.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import club.gskj.gulimall.product.service.AttrService;
import club.gskj.common.utils.PageUtils;
import club.gskj.common.utils.R;


/**
 * 商品属性
 *
 * @author 康纳-connor
 * @email 2417039231@gmail.com
 * @date 2020-12-27 15:07:43
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;


    /**
     * @param spuId 商品id
     * @param list  修改的 基本属性列表
     * @return club.gskj.common.utils.R
     * @author 康纳-connor
     * @creed: Talk is cheap,show me the code
     * @date 21.2.7 22:37
     * @Description
     */
    @PostMapping("/update/{spuId}")
    public R updateAttrBySpuId(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> list) {

        productAttrValueService.updateAttrBySpuId(spuId, list);
        return R.ok();
    }


    /***
     *
     * @param spuId  商品id
     * @return club.gskj.common.utils.R
     * @author 康纳-connor
     * @creed: Talk is cheap,show me the code
     * @date 21.2.7 16:27
     * @Description 根据商品id 查询所有基本属性列表
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R listBaseAttrForSpu(@PathVariable("spuId") Long spuId) {

        List<ProductAttrValueEntity> list = productAttrValueService.listBaseAttrForSpuBySpuId(spuId);

        return R.ok().put("data", list);
    }

    /**
     * @param catelogId 分类if
     * @param pathType  基本属性、销售属性
     * @param params    查询条件
     * @return club.gskj.common.utils.R
     * @author 康纳-connor
     * @creed: Talk is cheap,show me the code
     * @date 21.2.7 16:28
     * @Description 根据类型查询对应的 分组信息
     */
    @RequestMapping("/{pathType}/list/{catelogId}")
    public R baseAttrList(
            @PathVariable("catelogId") Long catelogId,
            @PathVariable("pathType") String pathType,
            @RequestParam Map<String, Object> params) {

        PageUtils page = attrService.queryBaseAttrPage(catelogId, params, pathType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
//        AttrEntity attr = attrService.getById(attrId);

        AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr) {
        attrService.save(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr) {

//        attrService.updateById(attr);
        attrService.updateAttrVo(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
